/*
 * 固定值设置
 * position:存放地理位置的文本框ID
 * mapButton:展开按钮ID
 * 
 */

//展开百度地图的标志
var mapFlag = false;
$('#mapButton').on('click',function(){
	mapFlag = !mapFlag;
	if(mapFlag){
		$('#mapButton').text("关闭地图");
		$('.mapCont').parent().parent().slideDown(300);
		$('.mapCont').slideDown(300);
		initBaiduMap();
	}else{
		$('#mapButton').text("展开地图");
		$('.mapCont').parent().parent().slideUp(300);
		$('.mapCont').slideUp(300);
		closeMap();
	}
});

var placeArr=[];
var placeArrLen=0;
var googleLocalList;
var autoable=true;
var autoHide;
var geocoder;
var strArr = [];
var lng;
var lat;

//关闭地图
function closeMap(){
	$("#map_canvas").html('<div class="tagMin" style="padding:5px;">Baidu地图加载中...</div>');
}

//显示目标地址信息
function showInfoWin(marker,myValue){
	var p=marker.getPosition(),myValue = myValue || "";
	var e=p.lng;
	var l=p.lat;
	var str=e+","+l;
	if(myValue==""){
		geocoder.getLocation(p, function(rs){
			var addComp = rs.addressComponents;
			myValue = rs.address;
			marker.openInfoWindow(new BMap.InfoWindow("<span class=\"tagMin\">地址："+myValue+"<br /><br/>经度："+e+"<br />纬度："+l+"<br /></span>"));
		});
	} else {
		marker.openInfoWindow(new BMap.InfoWindow("<span class=\"tagMin\">地址："+myValue+"<br /><br/>经度："+e+"<br />纬度："+l+"<br /></span>"));
	}
	window.returnValue=e+","+l;
	$("#position").val(window.returnValue);
}


function initialize() {
	map = new BMap.Map("map_canvas");
	var dLng = lng;
	dLng = dLng == "" ? centerMap.lng: parseFloat(dLng);
	var dLat = lat;
	dLat = dLat == "" ? centerMap.lat: parseFloat(dLat);
	var center = new BMap.Point(dLng, dLat);
	map.centerAndZoom(center, 15); 
	map.addControl(new BMap.NavigationControl()); 
	map.addControl(new BMap.NavigationControl());  
	map.addControl(new BMap.ScaleControl());  
	map.addControl(new BMap.OverviewMapControl()); 
	map.enableScrollWheelZoom();
	
	geocoder = new BMap.Geocoder();//初始化反向查找
	var marker = new BMap.Marker(center, {draggable: true});
	marker.enableDragging(true); // 设置标注可拖拽
	
	marker.addEventListener("dragstart", function() {
		map.closeInfoWindow();
	});
	marker.addEventListener("dragend", function() {
		showInfoWin(marker);
		
	});
	
	map.addOverlay(marker);
	showInfoWin(marker);
	
	
		
}

//初始化地图信息
function initBaiduMap(){
	lng = centerMap.lng;
	lat = centerMap.lat;
	//设置默认值
	var googlemapVal = $("#position").val();
	if (googlemapVal != "" && googlemapVal.indexOf(",") != -1) {
		strArr = googlemapVal.split(",");
		lng = strArr[0];
		lat = strArr[1];
	}
	setTimeout(function(){initialize();},1000);
	googleLocalList=$("#googleLocalList");
	googleLocalList.mouseleave(function(){
		autoHide=setTimeout(function(){googleLocalList.fadeOut(); },5000);
	}).mouseenter(function(){ clearTimeout(autoHide); });
	
	var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
		{"input" : "googleLocal"
		,"location" : map
	});
	var myValue;
	$('#tangram-suggestion--TANGRAM__3-main').css('z-index','10000');
	ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
	var _value = e.item.value;
		myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
		setPlace(myValue);
	});	
} ;

function setPlace(myValue){
	//map.clearOverlays();    //清除地图上所有覆盖物
	var local = new BMap.LocalSearch(map, { //智能搜索
	  onSearchComplete: myFun
	});
	function myFun(){
		var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
		map.centerAndZoom(pp, 18);
		var marker = new BMap.Marker(pp, {draggable: true});
		marker.enableDragging(true); // 设置标注可拖拽
		marker.addEventListener("dragstart", function() {
			map.closeInfoWindow();
		});
		
		marker.addEventListener("dragend", function() {
			showInfoWin(marker);
		});
		map.addOverlay(marker);
		showInfoWin(marker,myValue);
	}
	
	local.search(myValue);
}